﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace XNA_TD
{
    public class DrawPath : IRunable, IDraw
    {
        VertexPositionColor[] vertices;
        private static BasicEffect basicEffect = null;

        public DrawPath(Pfad pfad)
            : base(Priority.Effekt)
        {
            List<Vector2> positions = new List<Vector2>();
            positions.Add(new Vector2(0, 4));

            Vector2 next = new Vector2(0, 4);
            while (pfad.hasNext(next))
            {
                next = pfad.getNext(next);
                positions.Add(next);
                if (positions.Count > 100)
                    break;
            }

            vertices = new VertexPositionColor[positions.Count];
            for (var i = 0; i < positions.Count; i++)
            {
                vertices[i].Position = new Vector3(positions[i].X, .2f, positions[i].Y);
                vertices[i].Color = Color.White;
            }

            if (basicEffect == null)
            {
                basicEffect = new BasicEffect(graphics.GraphicsDevice);
                basicEffect.VertexColorEnabled = true;
            }
        }

        public void Draw()
        {
            DebugInfo.Instance.add("positions", vertices.Length);
            basicEffect.View = viewMatrix;
            basicEffect.Projection = projectionMatrix;
            foreach (var effect in basicEffect.CurrentTechnique.Passes)
            {
                effect.Apply();
                graphics.GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.LineStrip, vertices, 0, (int)((gameTime.TotalGameTime.TotalSeconds) % (vertices.Length - 1)) + 1);
            }
        }

        public override void Load()
        {
        }

        public override void UnLoad()
        {
        }
    }
}
